//
//  GRXMainController.m
//  1-MushroomStreet-自定义tabbar
//
//  Created by xmg on 15/12/8.
//  Copyright © 2015年 grx. All rights reserved.
//

#import "GRXMainController.h"

#import "GRXStrollVC.h"
#import "GRXBuyVC.h"
#import "GRXChatTVC.h"
#import "GRXMineVC.h"


#import "GRXTabBar.h"
#import "GRXNavigationController.h"

#import "GRXLoginRegistController.h"


#define GRXColor(r,g,b) [UIColor colorWithRed: (r) / 255.0 green:(g) / 255.0 blue:(b) / 255.0 alpha:1];

#define GRXTabButtonTitleFont [UIFont systemFontOfSize:12]
@interface GRXMainController ()

@end



@implementation GRXMainController

+ (void)load{
    
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];//获取本类中的所有item
    
    // 选中
    NSMutableDictionary *arrSel = [NSMutableDictionary dictionary];
    
    //颜色
    arrSel[NSFontAttributeName] = GRXTabButtonTitleFont;
//    arrSel[]
    arrSel[NSForegroundColorAttributeName] = GRXColor(252, 76, 112);
    [item setTitleTextAttributes:arrSel forState:UIControlStateSelected];//fc4c70
    
    // 设置tabBarButtin字体大小: 必须设置正常状态下,如果不设置,就会不好使.
    NSMutableDictionary *attrNor = [NSMutableDictionary dictionary];
    // 颜色
    attrNor[NSFontAttributeName] = GRXTabButtonTitleFont;
    attrNor[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    [item setTitleTextAttributes:attrNor forState:UIControlStateNormal];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [UINavigationItem ap]

    //1.添加子控制器
    [self addChildController];
    
    
    //2.设置子控制器
    [self setUpChildController];
    
    
    //3.用自定义的tabBar代替系统的tabBar
    /*注意:tabBar的子控件的添加是在willAppear的时候添加的*/
    GRXTabBar *tabBar = [[GRXTabBar alloc] init];
    
    //由于self.tabBar是只读的,所以我们需要利用KVC来进行替换
//    self.tabBar = tabBar;
    [self setValue:tabBar forKeyPath:@"tabBar"];
    
   
}


//在添加子控制器的时候,这里把普通控制器包装成导航控制器
- (void)addChildViewController:(UIViewController *)childController{
    GRXNavigationController *navi = [[GRXNavigationController alloc] initWithRootViewController:childController];
    [super addChildViewController:navi];
}


- (void)addChildController{
    //添加第一个控制器
    GRXStrollVC *strollVC = [[GRXStrollVC alloc] init];
    [self addChildViewController:strollVC];
    
    
    //添加第二个控制器
    GRXBuyVC *buyTVC = [[GRXBuyVC alloc] init];
    [self addChildViewController:buyTVC];
    
    //添加第三个控制器
    GRXChatTVC *chatVC = [[GRXChatTVC alloc] init];
    [self addChildViewController:chatVC];
    
    //添加第三个控制器
    GRXMineVC *mineVC = [[GRXMineVC alloc] init];
    [self addChildViewController:mineVC];
}


- (void)setUpChildController{

        //    NSLog(@"%@",self.childViewControllers);
        GRXNavigationController *nav = self.childViewControllers[0];
        nav.tabBarItem.title = @"逛逛逛";
        nav.tabBarItem.image = [UIImage imageNamed:@"icon_tabbar_timeline"];
        nav.tabBarItem.selectedImage = [UIImage imageNamedWithOriganlMode:@"icon_tabbar_timeline_selected"];
        
        
        GRXNavigationController *nav1 = self.childViewControllers[1];
        nav1.tabBarItem.title = @"买买买";
        nav1.tabBarItem.image = [UIImage imageNamed:@"icon_tabbar_buy"];
        nav1.tabBarItem.selectedImage = [UIImage imageNamedWithOriganlMode:@"icon_tabbar_buy_selected"];
        
    
        GRXNavigationController *nav3 = self.childViewControllers[2];
        nav3.tabBarItem.title = @"聊聊聊";
        nav3.tabBarItem.image = [UIImage imageNamed:@"feed_im_icon"];
        nav3.tabBarItem.selectedImage = [UIImage imageNamedWithOriganlMode:@"feed_im_icon_selected"];
        
        
        GRXNavigationController *nav4 = self.childViewControllers[3];
        nav4.tabBarItem.title = @"我我我";
        nav4.tabBarItem.image = [UIImage imageNamed:@"mgjme_tabbarItem_me"];
        nav4.tabBarItem.selectedImage = [UIImage imageNamedWithOriganlMode:@"mgjme_tabbarItem_me_selected"];

}

@end
